﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using FluentData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class AdverseEventTypeManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="adverseEventType"></param>
        /// <returns></returns>
        public static OperateResult Save(AdverseEventType adverseEventType)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();

                if (adverseEventType.ID == 0)
                {
                    adverseEventType.CreateUserID = loginUser.Personnel_id;
                    adverseEventType.CreateTime = DateTime.Now;
                    int typeID = AdverseEventTypeService.Add(adverseEventType);
                    if (typeID == 0)
                    {
                        resultInfo.Result = 0;
                    }
                }
                else
                {
                    AdverseEventType oldAdverseEventType = AdverseEventTypeService.GetQuerySingleByID(adverseEventType.ID);
                    oldAdverseEventType.Name = adverseEventType.Name;
                    oldAdverseEventType.Remark = adverseEventType.Remark;
                    int rows = AdverseEventTypeService.Update(oldAdverseEventType);
                    if (rows == 0)
                    {
                        resultInfo.Result = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult Delte(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                using (IDbContext context = Db.GetInstance().Context())
                {
                    context.UseTransaction(true);

                    AdverseEventTypeService.DelByID(id, context);
                    AdverseEventModeService.DelByTypeID(id, context);
                    AdverseEventReasonService.DelByTypeID(id, context);

                    context.Commit();

                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
